# coding:utf-8


def getDomain(url):
    """获取domain"""
    site = urlparse(url).netloc
    wordSite = site.split('.')
    if len(wordSite)>2 and (wordSite[-2] in ('com', 'net', 'org')):
        domain = '.'.join(wordSite[-3:])
    elif len(wordSite)>1:
        domain = '.'.join(wordSite[-2:])

    return domain

def strToUnicode(text, encoding=None):
    """str to unicode"""

    if encoding is None:
        encoding = 'utf-8'

    if isinstance(text, unicode):
        return text
    elif isinstance(text, str):
        return text.decode(encoding)
    else:
        raise TypeError('str_to_unicode must receive a str or unicode object, got %s' % type(text).__name__)


def unicodeToStr(text, encoding=None):
    """unicode to str"""

    if encoding is None:
        encoding = 'utf-8'

    if isinstance(text, str):
        return text
    elif isinstance(text, unicode):
        return text.encode(encoding)
    else:
        raise TypeError('unicode_to_str must receive a unicode or str object, got %s' % type(text).__name__)
    

